﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApp.Web.WcfUserServiceRef;


namespace WebApp.Web
{
    class WcfServiceRef
    {
        private static WcfServiceRef instance;
        public static WcfServiceRef Instance
        {
            get
            {
                return instance ?? (instance = new WcfServiceRef());
            }
        }

        public void Logout()
        {
            using (var userService = new WcfUserServiceClient())
            {
                var cookieManagerEndpointBehavior = new CookieManagerEndpointBehavior();
                userService.Endpoint.Behaviors.Add(cookieManagerEndpointBehavior);
                userService.SignOut();
            }
        }

        private void LoginWcfService()
        {
            using (var userService = new WcfUserServiceClient())
            {
                var cookieManagerEndpointBehavior = new CookieManagerEndpointBehavior();
                userService.Endpoint.Behaviors.Add(cookieManagerEndpointBehavior);
                userService.LoginAsWebAppInterfaceUser("WebSocket", "WebSocket2014");
            }

            //var systemService = OpenWcfServiceClient<SystemWcfServiceClient, ISystemWcfService>(serviceCategory);
            //DateTime server = systemService.GetServerDateTime();
            //var span = DateTime.Now - server;
            //if (!(span > new TimeSpan(0, -2, 0) && span < new TimeSpan(0, 2, 0)))
            //    throw new ApplicationException(string.Format("服务器时间为{0}，请检准本机电脑时间后再登录！", server.ToString("yyyy/MM/dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)));
        }


        //
        //public B2CDataServiceContext DataServiceContext
        //{
        //    get
        //    {
        //        string url = string.Format(@"http://{0}/B2CService/B2CDataService.svc", ip);
        //        var context = new B2CDataServiceContext(new Uri(url));
        //        context.IgnoreResourceNotFoundException = true;
        //        //context.IgnoreMissingProperties = true; //提供的数据比客户端数据更多,更新客户端解决
        //        context.SendingRequest += (sender, e) =>
        //        {
        //            e.RequestHeaders.Set(System.Net.HttpRequestHeader.Cookie, CookieContainerService.Instance.WcfServiceUserCookie);
        //            ((System.Net.HttpWebRequest)e.Request).AutomaticDecompression = (System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate);
        //        };
        //        return context;
        //    }
        //}


        //public const string ip = "127.0.0.1:8080";
        //public const string ip = "localhost:2012";
        //public const string serviceCategory = "WcfService";

        //static private readonly string urlWcf = string.Format(@"http://{0}", ip);
        //private TClient OpenWcfServiceClient<TClient, TInterface>(string serviceCategory)
        //    where TClient : System.ServiceModel.ClientBase<TInterface>, new()
        //    where TInterface : class
        //{
        //    var typeName = typeof(TClient).Name;
        //    string serviceName = typeName.Substring(0, typeName.Length - "Client".Length);
        //    var client = new TClient();
        //    var uri = string.Format(@"{0}/{1}/{2}.svc", urlWcf, serviceCategory, serviceName);
        //    var address = new System.ServiceModel.EndpointAddress(new Uri(uri));
        //    client.Endpoint.Address = address;

        //    //cookie和gzip
        //    var cookieManagerEndpointBehavior = new CookieManagerEndpointBehavior();
        //    //TcpTrace:client消息发往8111,trace转发到2898,service消息返给trace,trace返给client
        //    //Uri tcpTraceUri = new Uri(string.Format(@"{0}/{1}.svc", "http://localhost:8111", serviceName));
        //    //client.Endpoint.Behaviors.Add(new System.ServiceModel.Description.ClientViaBehavior(tcpTraceUri));
        //    client.Endpoint.Behaviors.Add(cookieManagerEndpointBehavior);

        //    ////设置 Binding
        //    //var textMsg = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
        //    //var gzip = new GZipEncoder.GZipMessageEncodingBindingElement(textMsg);
        //    //var http = new System.ServiceModel.Channels.HttpTransportBindingElement() { MaxReceivedMessageSize = 1024 * 1024 }; //1M
        //    //var customBinding = new System.ServiceModel.Channels.CustomBinding(gzip, http);
        //    //client.Endpoint.Binding = customBinding;

        //    return client;
        //}


    }
}
